home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
msqc25t1
/
hexcal.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-02-23
|
1KB
|
53 lines
/* hexcal.c by Tom Harrold feb., 24, 1990
This is a hexadecimal calculator program that uses strtoul
to read hexadecimal values from the command-line arguments.
It's designed to be invoked with the command line;
hexcal operand_1 operation operand_2, where hexcal is the name
of the program, operation is one of +,-,*, or /, and the
operand_1 and operand_2 are the hexadecimal oprands.
*/
#include <stdio.h>
#include <stdlib.h>
static char command[80] = " ";
main(int argc, char **argv)
{
unsigned long op1, op2;
if(argc < 4)
{
printf("Usage: %s <operand1> <operation> <operand2>\n", argv[0]);
}
op1 = strtoul(argv[1], (char **)NULL, 16);
op2 = strtoul(argv[3], (char **)NULL, 16);
switch (argv[2][0])
{
case '+':
printf("%lX (hex) ", op1 + op2);
printf("% 10.0lu (dec)\n", op1 + op2);
break;
case '-':
printf("%lX (hex) ", op1 - op2);
printf("% 10.0lu (dec)\n", op1 - op2);
break;
case '*':
printf("%lX (hex) ", op1 * op2);
printf("% 10.0lu (dec)\n", op1 * op2);
break;
case '/':
if(op2 == 0L)
{
printf("Can't divide by zero!\n");
}
else
printf("%lX (hex) ", op1 / op2);
printf("% 10.0lu (dec)\n", op1 / op2);
break;
}
}